package com.demo.studentmanager.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.studentmanager.dto.CourseAddDto;
import com.demo.studentmanager.dto.CourseListDto;
import com.demo.studentmanager.entity.Course;
import com.demo.studentmanager.entity.Teach;
import com.demo.studentmanager.exception.BizException;
import com.demo.studentmanager.result.ResultCode;
import com.demo.studentmanager.service.ICourseService;
import com.demo.studentmanager.service.ITeachService;
import com.demo.studentmanager.service.ITeacherService;
import com.demo.studentmanager.util.CheckEntityUtil;
import com.demo.studentmanager.vo.CourseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author ZHB
 * @version 1.0.0
 * @date Create in 2020/11/19
 */

@Api(tags = "课程模块")
@RequestMapping("/course")
@RestController
public class CourseController {

    @Autowired
    private ICourseService courseService;
    @Autowired
    private ITeachService teachService;
    @Autowired
    private ITeacherService teacherService;

    @RequiresAuthentication
    @GetMapping
    @ApiOperation(value = "课程信息")
    public IPage<CourseVO> getCourseList(@Validated CourseListDto courseListDto) {
        return courseService.getCourseList(courseListDto);
    }

    @RequiresRoles(value={"teacher","admin"},logical= Logical.OR)
    @PostMapping
    @ApiOperation(value = "新增课程")
    public void addCourse(@Validated CourseAddDto courseAddDto) {

        CheckEntityUtil.isNotNullById(courseAddDto.getCno(), courseService, ResultCode.COURSE_EXIST);
        CheckEntityUtil.isNullById(courseAddDto.getTno(), teacherService, ResultCode.TEACHER_ACCOUNT_NOT_EXIST);

        Teach teach = new Teach().setTno(courseAddDto.getTno()).setCno(courseAddDto.getCno());
        Wrapper<Teach> queryWrapper = new QueryWrapper<>(teach);
        if (teachService.getOne(queryWrapper) != null) {
            throw new BizException(ResultCode.TEACHER_TEACHING_EXIST);
        }

        Course course = new Course();
        courseAddDto.convertToEntity(course);

        courseService.save(course);

        teach.setCterm(courseAddDto.getCterm());

        teachService.save(teach);
    }

    @RequiresRoles(value={"teacher","admin"},logical= Logical.OR)
    @DeleteMapping("/{cno}")
    @ApiOperation("禁用课程")
    public void delSelectCourse(@NotNull @PathVariable String cno) {
        CheckEntityUtil.isNullById(cno, courseService, ResultCode.COURSE_NOT_EXIST);
        Teach teach = new Teach().setCno(cno);
        Wrapper<Teach> queryWrapper = new QueryWrapper<>(teach);
        teach=teachService.getOne(queryWrapper);
        if (teach==null) {
            throw new BizException(ResultCode.TEACHER_TEACHING_NOT_EXIST);
        }

        courseService.removeById(cno);

    }

}
